package org.grayrabbit.commons.dao;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;
/**
 * Page主要用于Web应用中的列表页面，主要包含到列表结果或者查询结果，包括信息有<tt>总的记录行数</tt>，当前页的<tt>起始行数，
 * 当前<tt>页码数</tt>，<tt>总页数</tt>同时可以控制,是否要进行总记录行数的统计
 * @author sys53
 * @version 1.0 2008-12-31 上午10:50:00
 * @since 1.0
 * @param <T>
 */
public class Page<T> {
	// 公共变量 //
	public static final String ASC = "asc";
	public static final String DESC = "desc";
	
	//结果集，初使用为空，否则struts2易出错
	private List<T> result  = Collections.emptyList();;
	
	//分页变量
	private int total = 0;	
	private int pageNo = 1;	
	private int pageSize = 1;
	private String orderBy;
	private String order;
	private boolean autoCount;
	
	public boolean isAutoCount() {
		return autoCount;
	}

	public void setAutoCount(boolean autoCount) {
		this.autoCount = autoCount;
	}

	public Page(){
		this.pageSize = 10;
	}
	public Page(final int pageSize) {
		this.pageSize = pageSize;
	}

	public Page(final int pageSize, final boolean autoCount) {
		this.pageSize = pageSize;
		this.autoCount = autoCount;
	}
	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getOffset() {
		if(pageNo<=0)
			pageNo = 1;
		return getRealPageSize() * (pageNo-1);
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(final int pageNo) {
		this.pageNo = pageNo;
		if(pageNo<1)
			this.pageNo = 1;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	private int getRealPageSize(){
		if(pageSize==-1){
			return this.total;
		}else if(pageSize<1){
			return 1; 
		}
		return this.pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		if (pageSize ==-1) {
			this.pageNo = 1;
		}else if(pageSize <1){
			this.pageSize = 10;
		}
	}
	
	/**
	 * 获得排序字段,无默认值.多个排序字段时用','分隔.
	 */
	public String getOrderBy() {
		return orderBy;
	}
	
	/**
	 * 设置排序字段,多个排序字段时用','分隔.
	 */
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	
	/**
	 * 是否已设置排序字段,无默认值.
	 */
	public boolean isOrderBySetted() {
		return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(order));
	}
	
	/**
	 * 获取排序片段，如:sql,hql,可自定义别名,alias进行组装
	 * @param alias 别名
	 */
	public String getOrderByString(String alias){
		if(StringUtils.isNotBlank(order) && StringUtils.isNotBlank(orderBy)){
			String[] orderArr = StringUtils.split(order,",");
			String[] orderByArr = StringUtils.split(orderBy,",");
			StringBuffer sb = new StringBuffer();
			for (int i=0;i<orderByArr.length;i++) {
				if(sb.length()>0)
					sb.append(',');
				if(StringUtils.isNotBlank(alias)){
					sb.append(alias).append(".");
				}
				sb.append(orderByArr[i]).append(" ").append(orderArr[i]);
			}
			return sb.toString();
		}
		return "";
	}
	/**
	 * 获得排序方向.
	 */
	public String getOrder() {
		return order;
	}


	/**
	 * 设置排序方式向.
	 * 
	 * @param order 可选值为desc或asc,多个排序字段时用','分隔.
	 */
	public void setOrder(final String order) {
		//检查order字符串的合法值
		String[] orders = StringUtils.split(StringUtils.lowerCase(order), ',');
		for (String orderStr : orders) {
			if (!StringUtils.equals(DESC, orderStr) && !StringUtils.equals(ASC, orderStr))
				throw new IllegalArgumentException("排序方向" + orderStr + "不是合法值");
		}

		this.order = StringUtils.lowerCase(order);
	}
	/**
	 * 根据pageSize与totalCount计算总页数, 默认值为-1.
	 */
	public long getTotalPages() {
		if (total < 0)
			return -1;
		if(pageSize<=0)
			return 1l;
		long count = total / getRealPageSize();
		if (total % getRealPageSize() > 0) {
			count++;
		}
		return count;
	}
	
	/**
	 * 是否还有下一页.
	 */
	public boolean isHasNext() {
		return (pageNo + 1 <= getTotalPages());
	}

	/**
	 * 取得下页的页号, 序号从1开始.
	 * 当前页为尾页时仍返回尾页序号.
	 */
	public int getNextPage() {
		if (isHasNext())
			return pageNo + 1;
		else
			return pageNo;
	}
	
	/**
	 * 是否还有上一页.
	 */
	public boolean isHasPre(){
		return (pageNo - 1 >= 1);
	}
	
	/**
	 * 取得上页的页号, 序号从1开始.
	 * 当前页为首页时返回首页序号.
	 */
	public int getPrePage() {
		if (isHasPre())
			return pageNo - 1;
		else
			return pageNo;
	}

	/**
	 * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从1开始.
	 */
	public int getFirst() {
		return ((pageNo - 1) * getRealPageSize()) + 1;
	}
}
